package com.jingyanzi.member.service.impl;

import java.util.Calendar;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.member.dao.MIntegralMapper;
import com.jingyanzi.member.domain.IntegralDO;
import com.jingyanzi.member.domain.result.ResultDO;
import com.jingyanzi.member.domain.result.ResultSupport;
import com.jingyanzi.member.entity.MIntegral;
import com.jingyanzi.member.service.IIntegralLogService;
import com.jingyanzi.member.service.IIntegralService;
import com.jingyanzi.member.util.BeanUtilsExtends;
import com.jingyanzi.member.util.MemberConstant;

@Service("integralService")
public class IntegralServiceImpl extends BaseServiceImpl implements IIntegralService {

	private static Logger logger = Logger.getLogger(IntegralServiceImpl.class);

	@Autowired
	private MIntegralMapper integralMapper;


	public ResultDO add(IntegralDO domain) {

		MIntegral entity = new MIntegral();

		ResultSupport result = BeanUtilsExtends.copy(entity, domain);
		if (!result.isSuccess()) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("会员积分信息处理失败");
			result.setSuccess(false);
			logger.error("会员积分DO对象转换失败");
			return result;
		}

		// 会员积分数据初始化
		entity.setTotleIntegral(0);
		entity.setUseIntegral(0);
		entity.setChangeNumber(0);
		entity.setExp(0);
		entity.setIsDelete(MemberConstant.DB_ISDEL_NO);
		entity.setCreatetime(Calendar.getInstance().getTime());
		entity.setModifitime(entity.getCreatetime());
		int r = -1;
		try {
			r = integralMapper.insert(entity);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("会员积分信息添加失败");
			result.setSuccess(false);
			logger.error("会员积分信息添加异常");
			return result;
		}
		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("会员积分信息添加失败");
			result.setSuccess(false);
			logger.error("会员积分信息添加失败");
			return result;
		}
		result.setModel(ResultDO.FIRST_MODEL_KEY, entity.getId());
		return result;
	}

	public ResultDO remove(Integer id) {

		ResultSupport result = new ResultSupport();

		if (isNumberInValid(id)) {
			result.setErrorCode(ResultDO.PARAM_ERROR);
			result.setErrorMsg("参数错误");
			result.setSuccess(false);
			logger.error("参数错误，id无效");
			return result;
		}

		MIntegral entity = new MIntegral();
		entity.setId(id);
		entity.setExp(0);
		entity.setTotleIntegral(0);
		entity.setIsDelete(MemberConstant.DB_ISDEL_YES);

		int r = -1;
		try {
			r = integralMapper.updateByPrimaryKeySelective(entity);
		} catch (Exception e) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("会员积分信息删除失败");
			result.setSuccess(false);
			logger.error("会员积分信息删除异常");
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("会员积分信息删除失败");
			result.setSuccess(false);
			logger.error("会员积分信息删除失败");
			return result;
		}

		return result;
	}

	public ResultDO modifi(IntegralDO domain) {
		MIntegral entity = new MIntegral();

		ResultSupport result = BeanUtilsExtends.copy(entity, domain);
		if (!result.isSuccess()) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("会员积分信息处理失败");
			result.setSuccess(false);
			logger.error("会员积分DO对象转换失败");
			return result;
		}
		entity.setModifitime(Calendar.getInstance().getTime());
		int r = -1;
		try {
			r = integralMapper.updateByPrimaryKeySelective(entity);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("会员积分信息更新失败");
			result.setSuccess(false);
			logger.error("会员积分信息更新异常");
			return result;
		}
		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("会员积分信息更新失败");
			result.setSuccess(false);
			logger.error("会员积分信息更新失败");
			return result;
		}
		return result;
	}

	public ResultDO get(Integer id) {

		ResultSupport result = new ResultSupport();

		if (isNumberInValid(id)) {
			result.setErrorCode(ResultDO.PARAM_ERROR);
			result.setErrorMsg("参数错误");
			result.setSuccess(false);
			logger.error("参数错误，id无效");
			return result;
		}

		MIntegral entity = null;
		try {
			entity = integralMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("会员积分信息查询失败");
			result.setSuccess(false);
			logger.error("会员积分信息查询失败");
			return result;
		}

		result.setModel(ResultSupport.FIRST_MODEL_KEY, getIntegralDO(entity));
		return result;
	}

}
